package com.xiaodimall.beforesys.controller;

import com.xiaodimall.beforesys.service.OrderService;
import com.xiaodimall.common.ServiceResultEnum;
import com.xiaodimall.utils.Result;
import com.xiaodimall.utils.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;


/**
 * @author wyc
 * @description
 * @ClassName OrderController
 * @date 2022/11/29 9:11
 */
@Controller
public class OrderController {


    //注入订单业务层模块
    @Resource
    OrderService orderService;



    /**
     * 根据订单号去查询出订单的信息
     *
     * @param request
     * @param orderNo
     * @param httpSession
     * @author WYC
     * @return: 返回到指定页面
     * @date: 2022/11/30 18:13
     */
    @ApiOperation(value = "根据订单号查询订单")
    @GetMapping("/orders/{orderNo}")
    public String orderDetailPage(HttpServletRequest request,
                                  @PathVariable("orderNo") String orderNo,
                                  HttpSession httpSession) {
        //调用订单业务层根据订单号查询订单功能
        return orderService.getOrderDetailByOrderNo(request, orderNo, httpSession);
    }



    /**
     * 保存订单
     * @author WYC
     * @param httpSession
     * @return: 返回到订单详情页面
     * @date: 2022/12/1 20:50
     */
    @ApiOperation(value = "保存订单")
    @GetMapping("/saveOrder")
    public String saveOrder(HttpSession httpSession) {
        String saveOrderResult = orderService.saveOrder(httpSession);
        //跳转到订单详情页
        return "redirect:/orders/" + saveOrderResult;
    }



    /**
     * 我的订单
     * @author WYC
     * @param params
     * @param request
     * @param httpSession
     * @return: 返回到我的订单页面
     * @date: 2022/12/1 20:50
     */
    @ApiOperation(value = "查看我的订单列表")
    @GetMapping("/orders")
    public String orderListPage(@RequestParam Map<String, Object> params,
                                HttpServletRequest request,
                                HttpSession httpSession) {
        //调用业务层查询出我的所有订单信息功能
        return orderService.getMyOrders(params, request, httpSession);
    }




    /**
     * 取消订单
     * @author WYC
     * @param orderNo
     * @param httpSession
     * @return: 返回结果集
     * @date: 2022/12/1 20:51
     */
    @ApiOperation(value = "手动取消订单")
    @PutMapping("/orders/{orderNo}/cancel")
    @ResponseBody
    public Result cancelOrder(@PathVariable("orderNo") String orderNo, HttpSession httpSession) {
        //调用订单业务层取消订单功能
        String cancelOrderResult = orderService.cancelOrder(orderNo, httpSession);

        if (ServiceResultEnum.SUCCESS.getResult().equals(cancelOrderResult)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(cancelOrderResult);
        }
    }


    /**
     * 确认订单到货
     * @author WYC
     * @param orderNo
     * @param httpSession
     * @return: 返回结果集
     * @date: 2022/12/1 20:51
     */
    @ApiOperation(value = "确认订单到货")
    @PutMapping("/orders/{orderNo}/finish")
    @ResponseBody
    public Result finishOrder(@PathVariable("orderNo") String orderNo, HttpSession httpSession) {
        //调用业务层确定商品到货功能
        String finishOrderResult = orderService.finishOrder(orderNo, httpSession);
        if (ServiceResultEnum.SUCCESS.getResult().equals(finishOrderResult)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(finishOrderResult);
        }
    }



}
